package com.meeting.order.controller;

import com.meeting.order.domain.User;
import com.meeting.order.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    
    /**
     * 用户登录接口
     * @param username 用户名
     * @param password 密码
     * @return 登录结果
     */
    @PostMapping("/login")
    public Map<String, Object> login(@RequestParam(required = false) String username, 
                        @RequestParam(required = false) String password) {
        Map<String, Object> response = new HashMap<>();
        
        // 参数校验
        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
            response.put("success", false);
            response.put("message", "用户名或密码不能为空");
            return response;
        }
        
        try {
            // 调用服务层进行登录验证
            String result = userService.login(username, password);
            if ("登录成功".equals(result)) {
                response.put("success", true);
                response.put("message", result);
            } else {
                response.put("success", false);
                response.put("message", result);
            }
        } catch (Exception e) {
            // 异常处理
            response.put("success", false);
            response.put("message", "登录失败");
        }
        
        return response;
    }
    
    /**
     * 用户注册接口
     * @param user 用户信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public Map<String, Object> register(@RequestBody(required = false) User user) {
        Map<String, Object> response = new HashMap<>();
        
        // 参数校验
        if (user == null || user.getUsername() == null || user.getUsername().isEmpty() || 
            user.getPassword() == null || user.getPassword().isEmpty()) {
            response.put("success", false);
            response.put("message", "用户名或密码不能为空");
            return response;
        }

        try {
            // 调用服务层进行注册
            String result = userService.register(user.getUsername(), user.getPassword());
            if ("注册成功".equals(result)) {
                response.put("success", true);
                response.put("message", result);
            } else {
                response.put("success", false);
                response.put("message", result);
            }
        } catch (Exception e) {
            // 异常处理
            response.put("success", false);
            response.put("message", "注册失败");
        }
        
        return response;
    }

}